from exts import db
from datetime import datetime


# 用户表
class UserModel(db.Model):
    __tablename__ = 'user'
    card_id = db.Column(db.String(100), primary_key=True, nullable=True, unique=True)
    username = db.Column(db.String(100), nullable=False)
    account_number = db.Column(db.String(50), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    phone = db.Column(db.String(100), nullable=True, unique=True)
    balance = db.Column(db.Numeric(6, 1), nullable=True)
    debt = db.Column(db.Boolean, default=False)
    join_time = db.Column(db.String(13))
    valid_date = db.Column(db.String(13))


# 管理员表
class AdminModel(db.Model):
    __tablename__ = "admin"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    admin_name = db.Column(db.String(100), nullable=False)
    account_number = db.Column(db.String(50), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    auth = db.Column(db.Integer, default=3)


# 图书表
class BookModel(db.Model):
    __tablename__ = "book"
    isbn = db.Column(db.String(13), primary_key=True, unique=True)
    book_name = db.Column(db.String(50), nullable=False)
    author = db.Column(db.String(100), nullable=True)
    press = db.Column(db.String(32), nullable=True)
    price = db.Column(db.Numeric(5, 1), nullable=False)
    type = db.Column(db.String(50), nullable=False)
    isLib = db.Column(db.Integer, nullable=True)


# 互借图书表
class InterloanBookModel(db.Model):
    __tablename__ = "interloan_book"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    lender = db.Column(db.String(100), nullable=False)
    state = db.Column(db.Integer, nullable=True)

    # 外键
    isbn = db.Column(db.String(13), db.ForeignKey('book.isbn'))


class InventoryModel(db.Model):
    __tablename__ = 'inventory'
    barcode = db.Column(db.String(6), primary_key=True)
    number = db.Column(db.Integer, nullable=False)
    borrowable_number = db.Column(db.Integer, nullable=False)
    storage_date = db.Column(db.String(13))
    location = db.Column(db.String(32))
    withdraw = db.Column(db.Boolean, default=False)  # 是否注销
    status = db.Column(db.Boolean, default=True)  # 是否在馆

    # 外键
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))  # 入库操作员
    isbn = db.Column(db.String(13), db.ForeignKey('book.isbn'))


class BorrowModel(db.Model):
    __tablename__ = "borrow"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    borrow_time = db.Column(db.String(13))
    return_time = db.Column(db.String(13))
    due_date = db.Column(db.String(13))

    # 外键
    book_id = db.Column(db.String(13), db.ForeignKey('book.isbn'))
    card_id = db.Column(db.String(100), db.ForeignKey('user.card_id'))
    barcode = db.Column(db.String(6), db.ForeignKey('inventory.barcode'))

    # 关系
    book = db.relationship(BookModel, backref='borrowBooks')
    borrower = db.relationship(UserModel, backref='borrowBooks')
    inventory = db.relationship(InventoryModel, backref='borrowBooks')


class DetailModel(db.Model):
    __tablename__ = "detail"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    author = db.Column(db.String(10))
    dept = db.Column(db.String(20))
    para1 = db.Column(db.String(255))
    para2 = db.Column(db.String(255))
    para3 = db.Column(db.String(255))
    para4 = db.Column(db.String(255))
    para5 = db.Column(db.String(255))
    para6 = db.Column(db.String(255))
    title = db.Column(db.String(60))

class LetterModel(db.Model):
    __tablename__ = "letter"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    author = db.Column(db.String(10))
    dept = db.Column(db.String(20))
    para1 = db.Column(db.String(255))
    title = db.Column(db.String(60))
